//
//  BlackShowView.m
//  DecentProject
//
//  Created by shaowei on 13-4-8.
//  Copyright (c) 2013年 vin. All rights reserved.
//

#import "BlackShowView.h"
#import "UIImageFactory.h"
#import "ViewShowAnimation.h"


static CGFloat sMinScale = 0.2f;
static CGFloat sMaxScale = 4.0f;

@interface BlackShowView()<UIScrollViewDelegate>


@end

@implementation BlackShowView

- (void)dealloc
{
    self.scrollView = nil;
    self.labelOfScale = nil;
    self.imageViewShow = nil;
    [super dealloc];
}

#pragma mark - 初始化方法

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image{
    
    self = [self initWithFrame:frame];
    if (self) {
        // Initialization code
        [self.imageViewShow setImage:image];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andImageName:(NSString *)imageName{
    self = [self initWithFrame:frame];
    if (self) {
        // Initialization code
        [self.imageViewShow setImage:[UIImageFactory getBundleImageWithName:imageName]];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andImageUrl:(NSString *)imageUrl{
    self = [self initWithFrame:frame];
    if (self) {
        // Initialization code
        [self.imageViewShow setImageWithUrl:imageUrl placeholderAnimation:YES];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    if (CGRectEqualToRect(frame, CGRectZero)) {
        frame.size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    }
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化所有设置
        [self allInit];
    }
    return self;
}

#pragma mark Function - Private

/**
 初始化所有设置
 */
- (void)allInit{
    
    //scroll初始化
    self.scrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
    [self addSubview:_scrollView];
    [_scrollView setAlwaysBounceHorizontal:YES];
    [_scrollView setAlwaysBounceVertical:YES];
    [_scrollView setMaximumZoomScale:sMaxScale];
    [_scrollView setMinimumZoomScale:sMinScale];
    [_scrollView setDelegate:self];
    
    //ImageView初始化
    CGRect frame = self.frame;
    CGFloat interval = INTERFACE_IS_PHONE?20.0f:40.0f;
    CGRect frameImage = CGRectZero;
    frameImage.origin = CGPointMake(interval, interval);
    frameImage.size = CGSizeMake(frame.size.width - interval*2,
                                 frame.size.height - interval*2);
    
    self.imageViewShow = [[[UIImageView alloc] initWithFrame:frameImage] autorelease];
    [self.scrollView addSubview:_imageViewShow];
    [self.imageViewShow setContentMode:UIViewContentModeScaleAspectFit];
    
    //初始化背景
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
    
    //添加关闭按钮
    [self addCloseButton];
    
    //添加缩放比例label
    [self addScaleLabel];
}

- (void)addScaleLabel{
    self.labelOfScale = [[[UILabel alloc] init] autorelease];
    _labelOfScale.backgroundColor = [UIColor clearColor];
    _labelOfScale.font = [UIFont boldSystemFontOfSize:20];
    _labelOfScale.textColor = [UIColor whiteColor];
    [_labelOfScale setTextAlignment:NSTextAlignmentCenter];
    CGFloat height = 50.0f;
    CGFloat width = 100.0f;
    _labelOfScale.frame = CGRectMake(self.frame.size.width*0.5f - width*0.5f,
                                     self.frame.size.height*0.5f - height*0.5f,
                                     width,
                                     height);
    [self addSubview:_labelOfScale];
    
}

- (void)addCloseButton{
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnSize = INTERFACE_IS_PHONE?45:70;
    closeBtn.frame = CGRectMake(self.frame.size.width - btnSize,
                                0,
                                btnSize,
                                btnSize);
    [closeBtn setTitle:@"X" forState:UIControlStateNormal];
    [closeBtn.titleLabel setTextColor:[UIColor whiteColor]];
    [closeBtn.titleLabel setFont:[UIFont systemFontOfSize:INTERFACE_IS_PHONE?13:22]];
    [closeBtn addTarget:self action:@selector(actionClose:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeBtn];
}

- (void)showScale:(CGFloat)scale{
    if (scale >=sMaxScale) {
        _labelOfScale.text = @"最大化";
    }else if (scale <= sMinScale){
        _labelOfScale.text = @"最小化";
    }else{
        _labelOfScale.text = [NSString stringWithFormat:@"%.0f %%",scale*100];
    }
    
    _labelOfScale.alpha = 1.0f;
    [UIView animateWithDuration:VIEW_ANIMATION_DURATION*2
                     animations:^{
                         _labelOfScale.alpha = 0.0f;
                     }];
}

#pragma mark - action

- (void)actionClose:(id)sender{
    [ViewShowAnimation hideView:self andIsRemoved:YES];
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _imageViewShow;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
    CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
    view.transform = transform;
    
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    //显示比例Label
    [self showScale:scrollView.zoomScale];
    
    //图片居中
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    
    _imageViewShow.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
    
}



@end
